//
//  DFPageView.m
//  换换圈
//
//  Created by 艾柯斯 on 14-11-13.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "DFPageView.h"


@interface DFPageView() <UIScrollViewDelegate> {
    NSMutableArray* _pageItems;
    UIScrollView* _scrollView;
    NSTimer* _carouselTimer;
}


@end

@implementation DFPageView


-(NSArray *)pageItems {
    return _pageItems;
}


-(void)setCurrentPageNumber:(NSInteger)pageNumber animated:(BOOL)animated {
    [UIView animateWithDuration:animated?_animationDuration:0 animations:^{
        self.currentPageNumber = pageNumber;
    }];
}

-(void)setCurrentPageNumber:(NSInteger)pageNumber {
     self.currentPageItem = _pageItems[pageNumber];
}

-(void)setCurrentPageItem:(UIView *)pageItem {
    NSInteger pageNumber = [_pageItems indexOfObject:pageItem];
    CGPoint contentOffset = _scrollView.contentOffset;
    contentOffset.x = pageNumber * _scrollView.frame.size.width;
    _scrollView.contentOffset = contentOffset;
    
    if([_delegate respondsToSelector:@selector(pageView:didPageNumberChangedFrom:to:)]) {
        [_delegate pageView:self didPageNumberChangedFrom:_currentPageNumber to:pageNumber];
    }
    
    if([_delegate respondsToSelector:@selector(pageView:didPageItemChangedFrom:to:)]) {
        [_delegate pageView:self didPageItemChangedFrom:_currentPageItem to:pageItem];
    }
    _currentPageNumber = pageNumber;
 
    _currentPageItem = pageItem;
}


-(void)setCurrentPageItem:(UIView *)pageItem animated:(BOOL)animated {
    
    [UIView animateWithDuration:animated?_animationDuration:0 animations:^{
        self.currentPageItem = pageItem;
    }];
}


-(NSUInteger)indexOfPageItem:(UIView *)pageItem {
    return  [_pageItems indexOfObject:pageItem];
}


-(void)reloadData {
    
    _pageItems= [NSMutableArray array];
    _animationDuration = 0.25;
    
    [_scrollView removeFromSuperview];
    _scrollView = [[UIScrollView alloc] initWithFrame:(CGRect){CGPointZero,self.frame.size}];
    _scrollView.delegate = self;
    // 关闭滚动条
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    // 开启分页
    _scrollView.pagingEnabled = YES;
    // 开启交互
    _scrollView.userInteractionEnabled = YES;
    // 开启滚动
    _scrollView.scrollEnabled = YES;
    // 添加
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self addSubview:_scrollView];
    
    NSInteger pageCount = [_dataSource numberOfPageItemInPageView:self];
    for (int i=0; i<pageCount; i++) {
        UIView* pageItem = [_dataSource pageView:self viewAtPageNumber:i];
        [_scrollView addSubview:pageItem];
        [_pageItems addObject:pageItem];
    }
}


-(void)layoutSubviews {
    [super layoutSubviews];
    for (int i=0; i<_pageItems.count; i++) {
        UIView* pageItem = _pageItems[i];
        pageItem.frame = CGRectMake(i * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
    }
    _scrollView.contentSize = CGSizeMake(_pageItems.count*_scrollView.frame.size.width, 0);
}

-(void)setDataSource:(id<DFPageViewDataSource>)dataSource {
    _dataSource = dataSource;
    [self reloadData];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.currentPageNumber = scrollView.contentOffset.x / scrollView.frame.size.width;
}


-(void)setCarousel:(BOOL)carousel timeInterval:(NSTimeInterval)timeInterval {
    if(_carousel==carousel) {
        return;
    }
    _carousel = carousel;
    [_carouselTimer invalidate];
    _carouselTimer = nil;
    if(carousel) {
        _carouselTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(handleCarousel) userInfo:nil repeats:YES];
    }
}



- (void)handleCarousel {
    if([_scrollView isDragging]) {
        return;
    }
    if(self.currentPageNumber<_pageItems.count-1) {
        [self setCurrentPageNumber:self.currentPageNumber+1 animated:YES];
    }else {
         [self setCurrentPageNumber:0 animated:YES];
    }
}

@end
